
import {find,hits} from '../../../../api/article';
const util = require('../../../../utils/util.js')
import css2canvas from "../../../../utils/css2canvas"
let app = getApp()

Page({
    data: {
        settime:null,
        content: '',
        title: '',
        article:{},
        articleId:'',
        canvasType: false,
        canvasHeight: 260,
        articleView:{},
        communityId:null,
        companyId:null
    },
    onLoad(options) {
        if(options.communityId){
            app.globalData.communityId = options.communityId
        }
        if(options.companyId){
            app.globalData.companyId = options.companyId
        }
    
        this.data.communityId=app.globalData.communityId
        this.data.companyId=app.globalData.companyId
        this.data.articleId = options.id
        this.setData({
            articleId:this.data.articleId,
            communityId:this.data.communityId,
            companyId:this.data.companyId
        })

        find(options.id).then(res => {
          this.data.article = res.data.article
          this.data.article.createDate = util.datetimeFormat(this.data.article.createDate)
          let text = res.data.article.content
          this.setData({
              title: res.data.article.title,
              content: text.replace(/\<img/gi, '<img style="max-width:100%;height:auto" '),
              article:this.data.article
          })

          this.doHits(options.id)
      })
       
    },
    onShow(){
        if(app.globalData.communityId || app.globalData.companyId){
            wx.showShareMenu({
                withShareTicket: true,
                menus: ['shareAppMessage', 'shareTimeline']
            })
        }else {
            wx.hideShareMenu({
                menus: ['shareAppMessage', 'shareTimeline']
            })
        }
    },
    onUnload() {
      if (this.data.settime!=null) {
        clearTimeout(this.data.settime);
      }
    },
    doHits(articleId){
      var _this = this;
        let params ={
            id:articleId,
            companyId:app.globalData.companyId,
            openId:app.globalData.openId
        }
        
        var readTime = 5000;
        if (this.data.article.readTime) {
          readTime = this.data.article.readTime*1000
        }
        this.data.settime = setTimeout(()=>{
          if (app.globalData.openId!=null) {
            hits(params).then(res =>{})
            clearTimeout(this.data.settime)
          } else {
            clearTimeout(this.data.settime)
            _this.doHits(articleId)
          }
        },readTime)

    },
    onShareAppMessage() {
        return {
            title: this.data.article.title,
            // desc: '为您挑选了一个好物',
            imageUrl: this.data.article.thumbnail + '?x-oss-process=image/resize,m_fixed,h_400,w_500',
            path: '/pages/articleDet/articleDet?id='+this.data.articleId + '&communityId=' + app.globalData.communityId + '&companyId=' + app.globalData.companyId
          }
    },
    starShare(){
        wx.updateShareMenu({
            templateInfo: {},
            withShareTicket: false,
            isPrivateMessage: true,//设置为私密
            activityId:  '321321321'//后端设置一个活动id，分享带上活动id
          })
    },
    canvasClick() {
        if(app.globalData.communityId != null){
            var  url  =  'http://fraud.rzico.com/1/'+app.globalData.communityId+'?id='+ this.data.articleId
        }
        if(app.globalData.companyId != null){
            var  url  =  'http://fraud.rzico.com/2/'+app.globalData.companyId+'?id='+ this.data.articleId
        }
        let  qrCode  =  app.APP_BASE_API +'/zxing/createImage?mchId=10200&data='+url
        var _this = this
        this.data.canvasType = true
        this.data.cardInfo ={
            Name: "****",
            TagText: "****",
            activityPrice: "",
            avater: "",
            coupons: "",
            logo: "http://cdnx.rzico.com/images/canvasMyLogo.png",
            marketPrice: "",
            price: "**",
            qrCode: qrCode
        }
        wx.getImageInfo({
          src:qrCode,
          success: res => {
            _this.data.carvasQrCode = res.path; //图片临时本地路径
            wx.getImageInfo({
              src: util.proxyImage(app.APP_BASE_API, util.thumbnail('http://cdn.rzico.com/img/fenxiang.png', 400, 400)), //网络图片路径
              success: res => {
                _this.data.carvasLogo = res.path; //图片临时本地路径
                _this.setData({
                  canvasType: _this.data.canvasType,
                  cardInfo: _this.data.cardInfo
                })
                let settime = setTimeout(function () {
                  _this.sharePosteCanvas()
                    clearTimeout(settime)
                }, 1000)
              }
            })
          }
        })
    },
    sharePosteCanvas: function () {
        var _this = this
        wx.showLoading({
          title: '生成中...',
          mask: true,
        })
        var canvasShow = wx.createCanvasContext("canvas-show")
        let ctx = new css2canvas(canvasShow)
        var canvasForm = [
          {
            type: "roundRect",
            x: 0,
            y: 0,
            width: 550,
            height: 500,
            background: "#ffffff",
            borderWidth: 1,
            borderRadius: 0,
            clip: false
          },
          {
            type: "image",
            imgUrl: _this.data.carvasQrCode,
            x: 20,
            y: 20,
            width: 400,
            height: 400
          },
          {
            type: "image",
            imgUrl: _this.data.carvasLogo,
            x: 60,
            y: 430,
            width: 40,
            height: 40
          },
          {
            type: "text",
            x: 112,
            y: 436,
            width: 320,
            content: '微信扫一扫，进入小程序',
            color: "#666666",
            fontSize: 24,
            textAlign: "left",
            overflow: "warp",
            maxLine: 1,
            lineHeight: 1
          },
    
        ]
        ctx
          .drawingView(canvasForm)
          .then(res => {
            console.log("绘制完成")
            wx.hideLoading();
          })
    },
    cancleCanvasClick() {
        this.data.canvasType = false
        this.setData({
          canvasType: this.data.canvasType
        })
    },

    saveShareImg: function () {
        var that = this;
        wx.showLoading({
          title: '正在保存',
          mask: true,
        })
        setTimeout(function () {
          wx.canvasToTempFilePath({
            canvasId: 'canvas-show',
            success: function (res) {
              wx.hideLoading();
              var tempFilePath = res.tempFilePath;
              wx.saveImageToPhotosAlbum({
                filePath: tempFilePath,
                success(res) {
                  wx.showModal({
                    content: '图片已保存到相册，赶紧晒一下吧~',
                    showCancel: false,
                    confirmText: '好的',
                    confirmColor: '#333',
                    success: function (res) {
                      if (res.confirm) { }
                    },
                    fail: function (res) { }
                  })
                },
                fail: function (res) {
                  wx.showToast({
                    title: res.errMsg,
                    icon: 'none',
                    duration: 2000
                  })
                }
              })
            }
          });
        }, 1000);
      },
    
    
})
